/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fork.nodeditor;

import fork.NodeEditor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import tal.drivechain.chain.ChainNode;
import tal.drivechain.chain.NodeExport;
import tal.drivechain.gui.GUIPopup;
import tal.drivechain.gui.TextButton;
import tal.drivechain.module.properties.ColorParser;

/**
 *
 * @author Tal Eisenberg
 */
public class NodeExportsPopup extends GUIPopup {

    NodeEditor owner;
    ChainNode node;
    TextButton[] buttons;
    TextButton applyButton, cancelButton;

    public NodeExportsPopup(NodeEditor owner, ChainNode node, Point location) {
        super(ColorParser.xor(owner.getColor()));
        this.owner = owner;
        this.node = node;

        this.setLocation(location);
        
        if (node.getNodeInfo().getProperties().isEmpty()) {
            JLabel l = new JLabel("Node doesn't have any properties to export.");
            l.setForeground(owner.getColor());
            l.setFont(owner.titleFont.deriveFont((int)Math.round(owner.titleFont.getSize()*1.1)));
            add(l);
            pack();
            return;
        }

        this.getContentPane().setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0; c.gridy = 0; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1;
        JLabel l = new JLabel("Select properties:   ");
        l.setForeground(owner.getColor());
        l.setFont(owner.titleFont.deriveFont(Font.BOLD, (float)(owner.titleFont.getSize()*1.1*1.1)));
        l.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        l.setHorizontalAlignment(JLabel.LEFT);
        add(l,c );
        JPanel buttonsPanel = new JPanel();
        int gwidth = (int)Math.round(Math.sqrt(node.getNodeInfo().getProperties().size()));
        int gheight = node.getNodeInfo().getProperties().size()/gwidth;
        buttonsPanel.setLayout(new GridLayout(0, Math.max(gwidth, gheight)));

        buttons = new TextButton[node.getNodeInfo().getProperties().size()];
        NodeExport e = node.getParentChain().getNodeExports().get(node);
        for (int i=0; i<buttons.length; i++) {
            buttons[i] = new TextButton("  " + node.getNodeInfo().getProperties().get(i).getName() + " ", ColorParser.xor(owner.getColor()), owner.titleFont.deriveFont(Font.PLAIN));
            buttons[i].setHasBorder(true);
            buttons[i].setNegative(false);
            buttons[i].setToggleButton(true);
            buttons[i].setBorder(BorderFactory.createLineBorder(ColorParser.xor(owner.getColor()).darker().darker()));
            buttons[i].setMargin(new Insets(2,1,2,1));
            if (e!=null) {
                for (String p : e.getProperties())
                    if (p.equals(buttons[i].getText().trim()))
                        buttons[i].setSelected(true);
            }
            buttonsPanel.add(buttons[i]);
        }

        c.gridx = 0; c.gridy = 1; c.fill = GridBagConstraints.BOTH; c.weightx = 1; c.weighty = 1; c.gridwidth = 2;
        add(buttonsPanel, c);

        c = new GridBagConstraints();
        c.gridx = 0; c.gridy = 2; c.weightx = 1; c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = 1;
        applyButton = new TextButton(" apply ", ColorParser.lighter(owner.getColor()), owner.titleFont);
        applyButton.setNegative(false);
        applyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                apply();
            }
        });
        add(applyButton, c);

        c.gridx = 1; c.gridy = 2; c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1; c.gridwidth = 1;
        cancelButton = new TextButton(" cancel ", owner.getColor(), owner.titleFont);
        cancelButton.setNegative(false);
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                closeWindow();
            }
        });
        add(cancelButton, c);

        pack();
    }

    private void apply() {
        List<String> lprops = new ArrayList<String>();
        for (TextButton b : buttons) if (b.isSelected()) lprops.add(b.getText().trim());
        if (lprops.isEmpty()) { if (node.getParentChain().getNodeExports().containsKey(node)) node.getParentChain().removeNodeExport(node); }
        else {
            NodeExport e = new NodeExport(node, lprops.toArray(new String[0]));
            node.getParentChain().addNodeExport(e);
        }
        closeWindow();
    }

    private void closeWindow() {
        setVisible(false);
        dispose();
    }
}
